﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace NRemedy.MVC.UI.Test
{
    [TestClass]
    //http://www.ibm.com/developerworks/cn/web/wa-lo-json/
    public class JsonBuilder_Test
    {
        [TestMethod]
        public void Single_Object_Json_Build_Test()
        {
            JsonBuilder builder = new JsonBuilder();
            DateTime dt = new DateTime(2013,5,1);
            builder.MergeProperty("property1", "string");
            builder.MergeProperty("property2", 1);
            builder.MergeProperty("property3", 3.14);
            builder.MergeProperty("property3", 3.15);//duplicate property should be override
            builder.MergeProperty("property5", null);
            builder.MergeProperty("property6", false);

            string reusult = builder.ToString();
            Assert.AreEqual(
                string.Format("{{{0}:{1},{2}:{3},{4}:{5},{6}:{7},{8}:{9}}}",
                    
                    "\"property1\"","\"string\"",
                    "\"property2\"", 1,
                    "\"property3\"", 3.15,
                    "\"property5\"", "null",
                    "\"property6\"", "false"
                ),
                reusult);
        }

        [TestMethod]
        public void Single_Object_Json_Build_Escape_Test()
        {
            JsonBuilder builder = new JsonBuilder();
            DateTime dt = new DateTime(2013, 5, 1);
            builder.MergeProperty("property1", "\"quote\"");
            builder.MergeProperty("property2", "\\/");
            builder.MergeProperty("property3", "\r");

            string reusult = builder.ToString();
            Assert.AreEqual(
                string.Format("{{{0}:{1},{2}:{3},{4}:{5}}}",

                    "\"property1\"", "\"\\\"quote\\\"\"",
                    "\"property2\"", "\"\\\\\\/\"",// src : \/  target : \\\/
                    "\"property3\"", "\"\\r\""//src : \r target: \r
                ),
                reusult);
        }

        [TestMethod]
        public void Complex_Object_Json_Build_Test()
        {
            JsonBuilder child = new JsonBuilder();
            child.MergeProperty("property1", "string");
            child.MergeProperty("property2", false);


            JsonBuilder parent = new JsonBuilder();
            parent.MergeProperty("property1", "parent");
            parent.MergeProperty("property2", 2);
            parent.MergeChildObject("children", child);
            parent.MergeChildObject("children", child);

            string reusult = parent.ToString();
            Assert.AreEqual(
                //{"property1":"parent","property2":2,"children":[{"property1":"string","property2":false},{"property1":"string","property2":false}]}
                "{\"property1\":\"parent\",\"property2\":2,\"children\":[{\"property1\":\"string\",\"property2\":false},{\"property1\":\"string\",\"property2\":false}]}"
               ,
                reusult);
        }
    }
}
